package com.css.fxfzxczh.modules.largeScreen.service.impl;

import com.css.fxfzxczh.base.attachment.entity.AttachmentInfoEntity;
import com.css.fxfzxczh.base.attachment.service.AttachmentInfoService;
import com.css.fxfzxczh.modules.disasterSituationDataManage.samPoiHouSur.entity.SamPoiHouSur;
import com.css.fxfzxczh.modules.disasterSituationDataManage.samPoiHouSur.repository.SamPoliHouSurRepository;
import com.css.fxfzxczh.modules.instrumentIntensity.entity.InstrumentIntensity;
import com.css.fxfzxczh.modules.instrumentIntensity.repository.InstrumentIntensityRepository;
import com.css.fxfzxczh.modules.largeScreen.queryParams.QuaParams;
import com.css.fxfzxczh.modules.largeScreen.service.LargeScreenService;
import com.css.fxfzxczh.modules.largeScreen.vo.FieldDataProportionVo;
import com.css.fxfzxczh.modules.largeScreen.vo.IntensityMapVo;
import com.css.fxfzxczh.modules.largeScreen.vo.TypicalDamagePhotoVo;
import com.css.fxfzxczh.modules.map.entity.DrawSeismicInfluence;
import com.css.fxfzxczh.modules.map.service.ShpDataService;
import com.css.fxfzxczh.modules.remoteSenseData.repository.RemoteSenseRepository;
import com.css.fxfzxczh.modules.remoteSenseData.vo.RemoteSenseVO;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.entity.EarEveManage;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.repository.EarEveManageRepository;
import com.css.fxfzxczh.util.UUIDGenerator;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author:
 * @CreateTime: 2024/5/8 15:38
 * @Description:
 */
@Service
public class LargeScreenServiceImpl implements LargeScreenService {

    @Resource
    private EarEveManageRepository earEveManageRepository;

    @Resource
    private ShpDataService shpDataService;

    @Resource
    private AttachmentInfoService attachmentInfoService;



    //抽样点房屋
    @Resource
    private SamPoliHouSurRepository samPoliHouSurRepository;


    //遥感数据
    @Resource
    private RemoteSenseRepository remoteSenseRepository;

    //仪器数据
    @Resource
    private InstrumentIntensityRepository instrumentIntensityRepository;



    @Override
    public EarEveManage getByCondition(QuaParams params) {
        EarEveManage byCondition = earEveManageRepository.getByCondition(params.getLongitude(), params.getLatitude(),
                params.getMagnitude(), params.getEventTime());
        return byCondition;
    }



    @Override
    public List<IntensityMapVo> intensityMapData() {
        List<IntensityMapVo> list=new LinkedList<>();
        //查询符合条件的地震事件
        List<EarEveManage> allEarEveManage = earEveManageRepository.getAllEarEveManage();
        if (CollectionUtils.isEmpty(allEarEveManage)){
            return Collections.emptyList();
        }
        for (EarEveManage earEveManage : allEarEveManage) {
            String xsemId = earEveManage.getXsemId();
            //查询对应烈度图空间数据
            List<IntensityMapVo> drawSeismicInfluenceList = shpDataService.getDrawSeismicInfluence(xsemId);
            if(!CollectionUtils.isEmpty(drawSeismicInfluenceList)){
                list.addAll(drawSeismicInfluenceList);
            }
        }
        return list;
    }

    @Override
    public List<TypicalDamagePhotoVo> getTypicalDamagePhoto() {
        List<TypicalDamagePhotoVo> list = new LinkedList<>();
        List<EarEveManage> allEarEveManage = earEveManageRepository.getAllEarEveManage();
        if (CollectionUtils.isEmpty(allEarEveManage)){
            return Collections.emptyList();
        }
        for (EarEveManage earEveManage : allEarEveManage) {
            String xsemId = earEveManage.getXsemId();
            List<SamPoiHouSur> samPoiHouSur = samPoliHouSurRepository.getHouseTypicalDamage(xsemId);
            if (CollectionUtils.isEmpty(samPoiHouSur)){
                continue;
            }else{
                for (SamPoiHouSur poiHouSur : samPoiHouSur) {
                    List<AttachmentInfoEntity> attachmentInfoEntities = attachmentInfoService.queryFile(poiHouSur.getHouseTypicalDamage());
                    if (!CollectionUtils.isEmpty(attachmentInfoEntities)) {
                        for (AttachmentInfoEntity attach : attachmentInfoEntities) {
                            TypicalDamagePhotoVo typicalDamagePhotoVo = new TypicalDamagePhotoVo();
                            typicalDamagePhotoVo.setId(UUIDGenerator.getUUID());
                            typicalDamagePhotoVo.setUrl(attach.getFileDirId()+ File.separator+attach.getAttachPath());
                            typicalDamagePhotoVo.setIaId(xsemId);
                            typicalDamagePhotoVo.setIntensity(poiHouSur.getIntensityDetermination());
                            list.add(typicalDamagePhotoVo);
                        }
                    }
                }
            }
        }
        return list;
    }


    @Override
    public List<FieldDataProportionVo> getFieldDataProportion() {

        List<FieldDataProportionVo> list = new LinkedList<>();
        List<EarEveManage> eveManageList = earEveManageRepository.getList();
        if (!CollectionUtils.isEmpty(eveManageList)){
            for (EarEveManage earEveManage : eveManageList) {
                String xsemId = earEveManage.getXsemId();
                List<SamPoiHouSur> samPoiHouSurList = samPoliHouSurRepository.getTaskListByXsemId(xsemId);
                List<RemoteSenseVO> remoteSenseList = remoteSenseRepository.getDataListByXsemId(xsemId);
                List<InstrumentIntensity> instrumentIntensityList = instrumentIntensityRepository.getInstrumentIntensityList(xsemId);

                FieldDataProportionVo fieldDataProportionVo = new FieldDataProportionVo();


                int total = samPoiHouSurList.size() + remoteSenseList.size() + instrumentIntensityList.size();
                fieldDataProportionVo.setId(UUIDGenerator.getUUID());
                fieldDataProportionVo.setIaId(xsemId);
                fieldDataProportionVo.setSurveyProportion(total == 0 ? 0 : (double) (samPoiHouSurList.size()/total));
                fieldDataProportionVo.setSatelliteProportion(total == 0 ? 0 : (double) (remoteSenseList.size()/total));
                fieldDataProportionVo.setInstrumentProportion(total == 0 ? 0 : (double) (instrumentIntensityList.size()/total));
                list.add(fieldDataProportionVo);
            }
        }

        return list;
    }
}
